package com.etfl.controller;

import com.etfl.entity.Admin;
import com.etfl.entity.Employee;
import com.etfl.entity.Weixiugong;
import com.etfl.service.IAdminService;
import com.etfl.service.IEmployeeService;
import com.etfl.service.IWeixiugongService;
import com.etfl.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 登录
 */
@Controller
@RequestMapping("/api/login")
public class LoginController {

    @Autowired
    private IAdminService iAdminService;

    @Autowired
    private IEmployeeService iEmployeeService;

    @Autowired
    private IWeixiugongService iWeixiugongService;

    /**
     * 管理员登录
     * @return
     */
    @RequestMapping("/author")
    @ResponseBody
    public Result author(HttpServletRequest request, HttpSession session) {
        String type=request.getParameter("type");
        String account=request.getParameter("account");
        String pwd=request.getParameter("pwd");
        if(type.equals("2")){//员工登录
            Employee employee=this.iEmployeeService.login(account,pwd);
            if(employee==null){
                return Result.reError("账户密码错误");
            }
            //保存登录数据
            session.setAttribute("employee",employee);
            session.removeAttribute("admin");
            session.removeAttribute("weixiugong");
            return Result.reOk("登录成功");
        }else if(type.equals("3")){//维修工登录
            Weixiugong weixiugong=this.iWeixiugongService.login(account,pwd);
            if(weixiugong==null){
                return Result.reError("账户密码错误");
            }
            //保存登录数据
            session.setAttribute("weixiugong",weixiugong);
            session.removeAttribute("admin");
            session.removeAttribute("employee");
            return Result.reOk("登录成功");
        }else{
            Admin admin=this.iAdminService.login(account,pwd,type);
            if(admin==null){
                return Result.reError("账户密码错误");
            }
            //保存登录数据
            session.setAttribute("admin",admin);
            session.removeAttribute("weixiugong");
            session.removeAttribute("employee");
            return Result.reOk("登录成功");
        }

    }

    /**
     * 获取当前登录的用户信息
     * @return
     */
    @RequestMapping("/getAccountInfo")
    @ResponseBody
    public Result getAccountInfo(HttpServletRequest request, HttpSession session) {
        String type=request.getParameter("type");
        if(type.equals("2")){
            if(session.getAttribute("employee")==null){
                return Result.reError("登录信息已过期");
            }
            Employee employee=(Employee)session.getAttribute("employee");
            session.removeAttribute("admin");
            session.removeAttribute("weixiugong");
            return Result.reOk("查询成功",employee);
        }else if(type.equals("3")){
            if(session.getAttribute("weixiugong")==null){
                return Result.reError("登录信息已过期");
            }
            Weixiugong weixiugong=(Weixiugong)session.getAttribute("weixiugong");
            session.removeAttribute("admin");
            session.removeAttribute("employee");
            return Result.reOk("查询成功",weixiugong);
        }else{
            if(session.getAttribute("admin")==null){
                return Result.reError("登录信息已过期");
            }
            Admin admin=(Admin)session.getAttribute("admin");
            session.removeAttribute("weixiugong");
            session.removeAttribute("employee");
            return Result.reOk("查询成功",admin);
        }
    }


    /**
     * 修改的用户信息
     * @return
     */
    @RequestMapping("/updateAccount")
    @ResponseBody
    public Result updateAccount(HttpServletRequest request, HttpSession session) {
        String type=request.getParameter("type");
        String username= request.getParameter("username");
        String nickname=request.getParameter("nickname");
        String pwd=request.getParameter("pwd");
        if(type.equals("2")){
            if(session.getAttribute("employee")==null){
                return Result.reError("登录信息已过期");
            }
            String age=request.getParameter("age");
            String phone=request.getParameter("phone");
            String address=request.getParameter("address");
            Employee employee=(Employee)session.getAttribute("employee");
            employee.setAccount(username);
            employee.setNickname(nickname);
            employee.setPwd(pwd);
            employee.setAge(Integer.parseInt(age));
            employee.setPhone(phone);
            employee.setAddress(address);
            this.iEmployeeService.updateById(employee);
            return Result.reOk("修改成功",employee);
        }else if(type.equals("3")){
            if(session.getAttribute("weixiugong")==null){
                return Result.reError("登录信息已过期");
            }
            String age=request.getParameter("age");
            String phone=request.getParameter("phone");
            String address=request.getParameter("address");
            Weixiugong weixiugong=(Weixiugong)session.getAttribute("weixiugong");
            weixiugong.setAccount(username);
            weixiugong.setNickname(nickname);
            weixiugong.setPwd(pwd);
            weixiugong.setAge(Integer.parseInt(age));
            weixiugong.setPhone(phone);
            weixiugong.setAddress(address);
            this.iWeixiugongService.updateById(weixiugong);
            return Result.reOk("修改成功",weixiugong);
        }else{
            if(session.getAttribute("admin")==null){
                return Result.reError("登录信息已过期");
            }
            Admin admin=(Admin)session.getAttribute("admin");
            if(admin==null){
                return Result.reError("登录信息已过期");
            }
            admin.setUsername(username);
            admin.setNickname(nickname);
            admin.setPwd(pwd);
            this.iAdminService.updateById(admin);
            return Result.reOk("修改成功",admin);
        }

    }



}
